var Moment = function (date) {
    var date;

    if (date) {
        this.date = new Date(date);
    } else {
        this.date = new Date();
    }

    return this;
};

Moment.prototype.byCrrTo = function () {
    var s = this.date;
    let now = new Date().getTime();
    let oldTime = s.getTime();
    let difference = now - oldTime;
    console.log(difference);
    let result = '';
    let minute = 1000 * 60;
    let hour = minute * 60;
    let day = hour * 24;
    let month = day * 30;
    let year = month * 12;

    let _year = difference / year;

    let _month = difference / month;

    let _week = difference / (7 * day);

    let _day = difference / day;

    let _hour = difference / hour;

    let _min = difference / minute;

    if (_year >= 1) {
        result = ~~_year + '年前';
    } else {
        if (_month >= 1) {
            result = ~~_month + '个月前';
        } else {
            if (_week >= 1) {
                result = ~~_week + '周前';
            } else {
                if (_day >= 1) {
                    result = ~~_day + '天前';
                } else {
                    if (_hour >= 1) {
                        result = ~~_hour + '个小时前';
                    } else {
                        if (_min >= 1) {
                            result = ~~_min + '分钟前';
                        } else {
                            result = '刚刚';
                        }
                    }
                }
            }
        }
    }

    return result;
};
/**
 * 对Date的扩展，将 Date 转化为指定格式的String
 * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 * eg:
 * "yyyy-MM-dd hh:mm:ss.S" ==> 2006-07-02 08:09:04.423
 * "yyyy-M-d h:m:s.S" ==> 2006-7-2 8:9:4.18
 * "yyyy-MM-dd E HH:mm:ss" ==> 2009-03-10 二 20:09:04
 * "yyyy-MM-dd EE hh:mm:ss" ==> 2009-03-10 周二 08:09:04
 * "yyyy-MM-dd EEE hh:mm:ss" ==> 2009-03-10 星期二 08:09:04
 */

Moment.prototype.format = function (format) {
    var date = this.date;
    /*
      var r= /^(\d{4})-(\d{1,2})-(\d{1,2})$/; //正则表达式 匹配出生日期(简单匹配)     
      r.exec('1985-10-15');
      s1=RegExp.$1;s2=RegExp.$2;s3=RegExp.$3;//结果为1985 10 15
      */

    if (typeof date === 'string') {
        date = this.parse(date);
    }

    var o = {
        'M+': date.getMonth() + 1,
        //月份
        '(d+|D+)': date.getDate(),
        //日
        '(h+|H+)': date.getHours(),
        //小时
        'm+': date.getMinutes(),
        //分
        's+': date.getSeconds(),
        //秒
        'q+': Math.floor((date.getMonth() + 3) / 3),
        //季度
        S: date.getMilliseconds() //毫秒
    };
    var week = {
        0: '日',
        1: '一',
        2: '二',
        3: '三',
        4: '四',
        5: '五',
        6: '六'
    };

    if (/(y+|Y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    if (/(E+)/.test(format)) {
        format = format.replace(RegExp.$1, (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay() + '']);
    }

    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
    }

    return format;
};

Moment.prototype.parse = function () {
    return this.date;
};
/**
 * 计算两个日期相差多少天
 * return day
 */

Moment.prototype.differ = function (date) {
    var time1 = this.date.getTime();

    if (typeof date === 'string') {
        date = new Date(date);
    }

    var time1 = this.date.getTime();
    var time2 = date.getTime();
    var differ = Math.ceil((time1 - time2) / (1000 * 3600 * 24)); //除不尽时,向上取整

    return differ;
};

Moment.prototype.add = function (num, optionType) {
    var date = this.date;

    if ('day' === optionType) {
        date.setDate(date.getDate() + num);
    }

    if ('month' === optionType) {
        date.setMonth(date.getMonth() + num);
    }

    if ('year' === optionType) {
        date.setFullYear(date.getFullYear() + num);
    }

    this.date = date;
    return this;
};

Moment.prototype.before = function (date) {
    return this.date.getTime() < new Date(date).getTime();
};

Moment.prototype.after = function (date) {
    return this.date.getTime() > date.getTime();
};

module.exports = function (date) {
    return new Moment(date);
};
